package com.briup.pts.web.controller;

import com.briup.pts.aop.Logger;
import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.QuarantineRegistration;
import com.briup.pts.service.QuarantineRegistrationService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wyq
 * @description TODO
 * @data 2025/5/14 15:01
 */
@Validated
@Tag(name = "质检报告管理模块")
@RestController
@RequestMapping("/quarantineRegistration")
@RequiredArgsConstructor
public class QuarantineRegistrationController {
    private final QuarantineRegistrationService qrService;

    @Operation(summary = "分页并根据条件查询检疫记录接口")
    @GetMapping("/query")
    public Result<PageInfo<QuarantineRegistration>> pageQuery(
            @RequestParam(defaultValue = "1")
            @Min(value = 1, message = "当前页码不能小于1") Integer pageNum,
            @RequestParam(defaultValue = "5")
            @Min(value = 1, message = "页面不能小于1") Integer pageSize,
            String bQualified, String grMechanism) {
        // 调用service层方法，获取分页数据
        PageInfo<QuarantineRegistration> pageInfo = qrService.pageQueryByConditional(
                pageNum, pageSize, bQualified, grMechanism);
        return Result.success(pageInfo);
    }

    @Logger("新增或更新检疫记录接口")
    @Operation(summary = "新增或更新检疫记录接口")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody @Valid QuarantineRegistration qr){
        qrService.saveOrUpdate(qr);
        return Result.success();
    }

    @Logger("根据检疫id查询检疫记录接口")
    @Operation(summary = "根据检疫id删除检疫记录接口")
    @DeleteMapping("/deleteById/{grId}")
    public Result<?> deleteById(@PathVariable Integer grId){
        qrService.deleteBatch(List.of(grId));
        return Result.success();
    }

    @Logger("批量删除检疫记录接口")
    @Operation(summary = "批量删除检疫记录接口")
    @DeleteMapping("/deleteByIdAll")
    public Result<?> deleteByIdAll(
            @RequestBody @NotEmpty(message = "需要删除的质检报告id集合不能为空") List<Integer> ids){
        qrService.deleteBatch(ids);
        return Result.success();
    }
}
